<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/22
 * Time: 16:45
 */

namespace app\common\model;

use think\Cache;
use XznsTools\cache\CacheManage;

class ZlAstrExerciseRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_astr_exercise_type_record';
    protected $cacheModel = 'astr_exercise_record';


    /**
     * 保存用户习题通关记录
     * @param array $data
     * @return array|bool|false|int
     * @throws \Exception
     */
    public function saveUserExerciseRecord($uid, $pid, $data = [])
    {
        try
        {
            $res = $this->save($data);
        }
        catch (\Exception $e)
        {
            $this->error = '答题记录保存失败';
            return false;
        }

        //若考核超过60，则更新通过考核的缓存自增
        if ($data['grade'] == 60 || $data['grade'] > 60)
        {
            $this->SaveUserExerciseRecordStatus($uid, $pid);
        }
        //更新今日用户考核次数自增
        $this->SaveUserExerciseRecordNum($uid, $pid);
        return $res;
    }

    /**
     * 用户通过次数自增
     * @throws \Exception
     */
    public function SaveUserExerciseRecordStatus($uid, $pid, $key = 'id')
    {
        // 更新 订单数 缓存
        CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid . '-' . $pid)
            ->hashDataInc($key, 1, function () use ($uid, $pid, $key)
            {
                return $this->getUserExerciseRecordStatusFromDms($uid, $pid);
            });
    }

    /**
     * 缓存获取用户考核状态，超过60即为通过
     * @param $uid
     * @param int $score
     * @throws \Exception
     */
    public function getUserExerciseRecordStatus($uid, $type, $key = 'id')
    {
        $num = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid . '-' . $type)
            ->getDataAndSet(function () use ($uid, $type, $key)
            {
                $orderNum = $this->getUserExerciseRecordStatusFromDms($uid, $type);
                return [$key => $orderNum];
            }, [$key]);
        $num = is_array($num) ? $num[$key] : $num;
        return $num;
    }

    /**
     * 数据库获取用户考核状态，超过60即为通过
     * @param $uid
     * @param $type
     * @param int $score
     * @return array|bool
     */
    public function getUserExerciseRecordStatusFromDms($uid, $type, $score = 60)
    {
        $where = [
            'uid' => $uid,
            'cet_id' => $type,//类型 【1=>达人 2=>占星  3=>提问】
            'grade' => [['=', $score], ['>', $score], 'or']
        ];
        try
        {
            $data = $this->where($where)->count();
        }
        catch (\Exception $e)
        {
            $this->error = '获取用户习题状态失败';
            return false;
        }

        return $data;
    }

    /**
     * 更新今日答题次数,自增
     * @throws \Exception
     */
    public function SaveUserExerciseRecordNum($uid, $pid, $key = 'id')
    {
        $start_time = strtotime(date('Y-m-d', time()) . ' 00:00:00');
        // 更新 订单数 缓存
        CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid . '-' . $pid . '-' . $start_time)
            ->hashDataInc($key, 1, function ()
            {
                return 1;
            });
    }

    /**
     * 缓存获取用户今天习题次数
     * @param int $uid
     * @return bool|false|mixed|\PDOStatement|string|\think\Collection
     * @throws \Exception
     */
    public function getUserExerciseRecordNum($uid, $type, $key = 'id')
    {
        $start_time = strtotime(date('Y-m-d', time()) . ' 00:00:00');

        $num = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid . '-' . $type . '-' . $start_time)
            ->getDataAndSet(function () use ($key)
            {
//                $orderNum = $this->getUserExerciseRecordNumFromDms($uid, $start_time, $end_time, $type);
                return [$key => 1];
            }, [$key]);
        $num = is_array($num) ? $num[$key] : $num;
        return $num;
    }

    /**
     * 数据库获取今日答题次数
     * @param $uid
     * @param $start_time
     * @param $end_time
     * @return bool | array
     */
    public function getUserExerciseRecordNumFromDms($uid, $start_time, $end_time, $type)
    {
        $where = [
            'uid' => $uid,
            'cet_id' => $type,//类型 【1=>达人 2=>提问  3=>占星】
            'create_time' => ['between', [$start_time, $end_time]]
        ];
        try
        {
            $data = $this->where($where)->count();
        }
        catch (\Exception $e)
        {
            $this->error = '获取用户习题状态失败';
            return false;
        }

        return $data;
    }
}
